package top.jle.spring.basic.config;

import org.hibernate.cfg.AvailableSettings;
import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode;
import org.hibernate.tool.schema.Action;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernatePropertiesCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;

@AutoConfiguration
@Import({JacksonConfig.class,WebConfig.class, TransactionConfig.class})
public class JleSpringBasicAutoConfiguration {

    @Bean
    public HibernatePropertiesCustomizer hibernatePropertiesCustomizer(){
        return hibernateProperties -> {
            hibernateProperties.put(AvailableSettings.ALLOW_UPDATE_OUTSIDE_TRANSACTION,true);
            hibernateProperties.put(AvailableSettings.INTERCEPTOR,"top.jle.spring.basic.interceptor.EntityInterceptor");
            hibernateProperties.put(AvailableSettings.HBM2DDL_AUTO, Action.ACTION_UPDATE);
            hibernateProperties.put(AvailableSettings.JAKARTA_HBM2DDL_CREATE_SCHEMAS,true);
            hibernateProperties.put(AvailableSettings.CONNECTION_HANDLING, PhysicalConnectionHandlingMode.DELAYED_ACQUISITION_AND_RELEASE_AFTER_TRANSACTION);
            hibernateProperties.put(AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS,"org.springframework.orm.hibernate5.SpringSessionContext");
            hibernateProperties.put("hibernate.temp.use_jdbc_metadata_defaults",false);
        };
    }
}
